<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title></title>
</head>

<body>
	<script type="text/javascript">
		//1.push() 添加元素到数组结尾  返回数组的长度
		let arr = [1, 2, 3]
		let plres = arr.push(10)

		console.log("plres -----", arr, plres) //[1, 2, 3,10] 4

		//2.unshift() 添加元素到开头 返回数组元素
		let u1res = arr.unshift("第一")
		console.log("plres -----", arr, u1res) //"第一",1, 2, 3,10] 5

		//3.pop() 删除结尾的元素 返回删除元素
		let arrPoP = [3, 4, 5, "zuihou"]
		let popRes = arrPoP.pop()
		console.log(arrPoP, popRes) // [3, 4, 5]zuihou

		//4.shift() 删除数组开头的元素 返回删除的元素

		let sres = arrPoP.shift()
		console.log(arrPoP, sres) // [ 4, 5] 3

		//5.splice(开始的位置（下标）,截取的长度) 返回截取的内容
		let spArr = [1, 2, 3, 5, 6]
		// let spRes = spArr.splice(1,2)//[1,5,6] [2,3]
		//一个参数时表示从这个下标一直删到最后
		// let spRes = spArr.splice(1) //[1] [2,3,5,6]
		//第一个参数是负的 表示倒着数 -1表示倒数第一个
		// let spRes = spArr.splice(-3,3)
		// console.log("spres",spArr, spRes) //[1,2] [3,5,6]

		//6.splice(开始的位置（下标）,截取的长度，添加元素) 返回截取的内容并替换

		let spres2 = spArr.splice(0, 1, "a", "b", "c")
		console.log("spres2", spArr, spres2)//['a', 'b', 'c', 2, 3, 5, 6]


		// 7 concat() 合并数组 返回新数组
		let conArr = [1, 2, 3]
		let conArr2 = [1, 2, 3]
		let conRes = conArr.concat(conArr2)	//[1, 2, 3, 1, 2, 3]

		console.log("conres---", conArr, conArr2, conRes)

		//8 sort() 排序
		/*
			排序原理：先利用toString()转成字符串，让字符串按照Unicode表进行比较，根据大小进行排序
			怎么看Unicode值--------使用charcodeAt()
		*/

		let sortArr = [2, 3, 1]
		console.log("sortArr", sortArr.sort()) //[1, 2, 3]
		let sortArr2 = [3, 20, 2]
		console.log("sortArr2", sortArr2.sort()) //[2, 20, 3]
		let sortArr3 = ["cat", "dog", "pig"]
		console.log("sortArr3", sortArr3.sort())//['cat', 'dog', 'pig']
		console.log("cat".charCodeAt()) //99
		console.log("dog".charCodeAt()) //100
		console.log("pig".charCodeAt()) //112+


		//自定义排序规则 用法 数组.sort(自定义方法名)
		//升序排列 从小到大

		function compareAsc(a, b) {
			if (a < b) {
				return -1 //表示a放在b前面
			} else if (a === b) {
				return 0
			} else {
				return 1 //表示a放在b后面 
			}
		}

		console.log("升序-------", sortArr2.sort(compareAsc))
		//	降序
		function compareDesc(a, b) {
			if (a < b) {
				return 1 //表示a放在b后面 
			} else if (a === b) {
				return 0
			} else {
				return -1 //表示a放在b前面	
			}
		}
		console.log("降序-------", sortArr2.sort(compareDesc))


		//9.reverse()将数组中的元素反转
		let revArr = ['a', 1, 2, 'b']
		console.log("revArr---", revArr.reverse()) //['b', 2, 1, 'a']
		//10.数组转字符串 toString String

		console.log("转字符串", String(revArr), revArr.toString())

		//11 join()拼接字符串
		console.log("join------", revArr.join("-")) //b-2-1-a
		console.log("join------", revArr.join("*"))// b*2*1*a
		console.log("join------", revArr.join("")) //b21a

		//12 indexOf() 返回某个字符首次出现的位置
		let str = "1241skdlajglaagkdlj"
		let arr1 = [1, 3, 5, 4]
		console.log("----indexOf", str.indexOf('s'))
		console.log("----indexOf", arr1.indexOf(4)) //3
		console.log("----indexOf", arr1.indexOf(99)) //-1 不存在

		// 13 charAt() 返回指定位置的字符
		console.log('chatAt-----', str.charAt(3))

		//14 split() 字符串拆分成数组
		let str2 = "还有三周过-五一"
		console.log(str2.split("-"))
		let url = 'https://www.baidu.com?userName=张三&passWord=123456&like=孙悟空'
		//张三 123456 孙悟空
		let url1 = url.split('?')[1].split('&')
		console.log(url1)
		for (let i = 0; i < url1.length; i++) {
			console.log(url1[i].split('=')[1])
		}


		//splice split slice
		//15 slice(start,end) 截取数组中的元素 左闭右开 [,) 不包含end
		let arr5 = ["h", "e", "l", "l", "o"]
		console.log(arr5.slice(1, 3)) //['e','l']
		console.log(arr5.slice(1)) //['e','l','l','o']

		//16 如何判断数组

		console.log(Array.isArray(arr5))

		//变量 instanceof 数据类型
		console.log(arr5 instanceof String) //false
	</script>
</body>

</html>